{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Largest Substring Between Two Equal Characters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: maxLengthBetweenEqualCharacters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #两个相同字符之间的最长子字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串 <code>s</code>，请你返回 <strong>两个相同字符之间的最长子字符串的长度</strong> <em>，</em>计算长度时不含这两个字符。如果不存在这样的子字符串，返回 <code>-1</code> 。</p>\n",
    "\n",
    "<p><strong>子字符串</strong> 是字符串中的一个连续字符序列。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"aa\"\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>最优的子字符串是两个 'a' 之间的空子字符串。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"abca\"\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>最优的子字符串是 \"bc\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"cbzxy\"\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>s 中不存在出现出现两次的字符，所以返回 -1 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"cabbac\"\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>最优的子字符串是 \"abba\" ，其他的非最优解包括 \"bb\" 和 \"\" 。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 300</code></li>\n",
    "\t<li><code>s</code> 只含小写英文字母</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [largest-substring-between-two-equal-characters](https://leetcode.cn/problems/largest-substring-between-two-equal-characters/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [largest-substring-between-two-equal-characters](https://leetcode.cn/problems/largest-substring-between-two-equal-characters/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"aa\"', '\"abca\"', '\"cbzxy\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "from typing import DefaultDict, List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        pos: DefaultDict[str, List[int]] = defaultdict(list)\n",
    "        for i, c in enumerate(s):\n",
    "            pos[c].append(i)\n",
    "            \n",
    "        return max(\n",
    "            ((p[-1] - p[0] - 1) for c, p in pos.items() if len(p) > 1),\n",
    "            default=-1\n",
    "        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        res = -1\n",
    "        ls = [-1]*26\n",
    "        for i in range(len(s)):\n",
    "            idx = ord(s[i])-97\n",
    "            if ls[idx] != -1:\n",
    "                res = max(res, i-ls[idx]-1)\n",
    "            else:\n",
    "                ls[idx] = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        \"\"\"\n",
    "        abjklib123\n",
    "        \"\"\"\n",
    "        dct = {}\n",
    "        res = -1\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in dct:\n",
    "                dct[s[i]] = i\n",
    "            else:\n",
    "                res = max(res, i-dct[s[i]]-1)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        rec = dict()\n",
    "        ans = -1\n",
    "        for k,v in enumerate(s):\n",
    "            if v not in rec:\n",
    "                rec[v] = k\n",
    "            else:\n",
    "                ans = max(ans,k-rec[v]-1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        res=[]\n",
    "        for i in range(len(s)):\n",
    "            for j in range(i,len(s)):\n",
    "                count = 0\n",
    "                if s[i]==s[j]:\n",
    "                    count=j-i-1\n",
    "                    res.append(count)\n",
    "        return max(res)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        d = {}\n",
    "        res = -1\n",
    "        for i in range(len(s)):\n",
    "            if s[i] in d:\n",
    "                res = max(res,i-d[s[i]]-1)\n",
    "            else:\n",
    "                d[s[i]] = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        ma=[]\n",
    "        for i in range(len(s)):\n",
    "            if s.count(s[i])==1:\n",
    "                continue\n",
    "            for j in range(i+1,len(s)):\n",
    "                if s[i]==s[j]:\n",
    "                    ma.append(len(s[i+1:j]))\n",
    "        return -1 if len(ma)==0 else max(ma)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        ans = -1\n",
    "        firstIndex = {}\n",
    "        for i, c in enumerate(s):\n",
    "            if c not in firstIndex:\n",
    "                firstIndex[c] = i\n",
    "            else:\n",
    "                ans = max(ans, i - firstIndex[c] - 1)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        charAt = dict()\n",
    "        res = -1\n",
    "        for index, c in enumerate(s):\n",
    "            if charAt.get(c) is not None:\n",
    "\n",
    "                res = max(index-charAt[c]-1, res)\n",
    "            else:\n",
    "                charAt[c] = index\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        maxnum=-1\n",
    "        lst=list(s)\n",
    "        for i in lst:\n",
    "            for j in lst[::-1]:\n",
    "                if i==j:\n",
    "                    maxnum=max(-lst.index(i)-lst[::-1].index(j)+len(lst)-2,maxnum)\n",
    "                    break\n",
    "        return maxnum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        d = dict()\n",
    "        ans = -1\n",
    "        for i, c in enumerate(s):\n",
    "            if c not in d:\n",
    "                d[c] = i + 1\n",
    "            else:\n",
    "                ans = max(ans, i - d[c])\n",
    "        return ans\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        #暴力法\n",
    "        n = len(s)\n",
    "        maxlen = -1\n",
    "        for i in range(n):\n",
    "            for j in range(n - 1, i, -1):\n",
    "                if s[i] == s[j]:\n",
    "                    maxlen = max(maxlen, j - i - 1)\n",
    "        return maxlen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        alp = [chr(i) for i in range(97,123)]\n",
    "        ans = []\n",
    "        if len(set(s)) == len(s):\n",
    "            return -1\n",
    "        for i in alp:\n",
    "            if i in s:\n",
    "                if s.rfind(i) == s.find(i):\n",
    "                    ans.append(0)\n",
    "                else:\n",
    "                    ans.append(s.rfind(i) - s.find(i) - 1)\n",
    "        return max(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        return max(s.rfind(c) - s.find(c) - 1 for c in string.ascii_lowercase)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        maxs = -1\n",
    "        for i in range(len(s)):\n",
    "            for j in range(1,len(s)):\n",
    "                if s[i] == s[j]:\n",
    "                    maxs = max(maxs, j - i - 1)\n",
    "        return maxs\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        res = -1\n",
    "        ls = [-1]*26\n",
    "        for i in range(len(s)):\n",
    "            idx = ord(s[i])-97\n",
    "            if ls[idx] != -1:\n",
    "                res = max(res, i-ls[idx]-1)\n",
    "            else:\n",
    "                ls[idx] = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        s_map = dict()\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in s_map.keys():\n",
    "                s_map[s[i]] = []\n",
    "            s_map[s[i]].append(i)\n",
    "        \n",
    "        result = -1\n",
    "        for key in s_map.keys():\n",
    "            tmp = max(s_map[key]) - min(s_map[key]) - 1\n",
    "            if tmp > result:\n",
    "                result = tmp\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        maxlen=-1\n",
    "        x=dict()\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in x:\n",
    "                x[s[i]]=i\n",
    "            else:\n",
    "                maxlen=max(maxlen,i-x[s[i]]-1)\n",
    "        return maxlen\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        maxs = -1\n",
    "        for i in range(len(s)):\n",
    "            for j in range( i+1,len(s)):\n",
    "                if s[i] == s[j]:\n",
    "                    maxs = max(maxs, j - i - 1)\n",
    "        return maxs\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        max_result = -1\n",
    "        result_list = []\n",
    "        i = 0\n",
    "        n  = len(s) - 1\n",
    "        for i in range(n):\n",
    "            flag = False\n",
    "            for j in range(n, i, -1):\n",
    "                if s[i] == s[j]:\n",
    "                    flag = True\n",
    "                    result_list.append(j - i - 1)\n",
    "                    break\n",
    "                continue\n",
    "                \n",
    "            if not flag:\n",
    "                result_list.append(-1)\n",
    "        else:\n",
    "            for result in result_list:\n",
    "                max_result = max(max_result, result)\n",
    "\n",
    "        return max_result\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        max_len = -1\n",
    "        for i in range(n):\n",
    "            ind = s.rfind(s[i])\n",
    "            max_len = max(max_len,ind-i-1)\n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        smap = {}\n",
    "        res = 0\n",
    "        for i,v in enumerate(s):\n",
    "            if v not in smap.keys():\n",
    "                smap[v] = i\n",
    "            else:\n",
    "                res = max(res,i-smap[v]+1)\n",
    "        return max(-1,res-2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        if len(s) <= 1 or len(set(s)) == len(s):\n",
    "            return -1\n",
    "        else:\n",
    "            ans = 0\n",
    "            for idx, item in enumerate(s):\n",
    "                last_idx = s.rfind(item)\n",
    "                if last_idx > idx and last_idx - idx - 1 > ans:\n",
    "                    ans = last_idx - idx - 1\n",
    "            return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        ans = -1\n",
    "        for i in range(len(s)):\n",
    "            for j in range(len(s)-1,i,-1):\n",
    "                if s[i] == s[j]:\n",
    "                    ans = max(ans,j-i-1)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        maxlen = -1\n",
    "        n = len(s)\n",
    "        for i in range(n):\n",
    "            for j in range(n-1, i, -1):\n",
    "                if s[i] == s[j]:\n",
    "                    if j-i-1 >= maxlen:\n",
    "                        maxlen = j-i-1\n",
    "                    # maxlen = max(maxlen, j-i-1)\n",
    "        return maxlen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "\n",
    "        ans=-1\n",
    "        for left in range(len(s)):\n",
    "            if len(s)-left<ans:\n",
    "                break\n",
    "            for right in range(len(s)-1,left,-1):\n",
    "                if right-left-1<ans:\n",
    "                    break\n",
    "                if s[left]==s[right]:\n",
    "                    ans=right-left-1\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        slen = len(s)\n",
    "        dic = dict()\n",
    "        for i in range(slen):\n",
    "            if s[i] not in dic:\n",
    "                dic[s[i]] = [i]\n",
    "            else:\n",
    "                dic[s[i]] += [i]\n",
    "\n",
    "        res = -1\n",
    "\n",
    "        for key,val in dic.items():\n",
    "            if len(val) > 1:\n",
    "                res = max(res,val[-1]-val[0]-1)\n",
    "        return res\n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        h={}\n",
    "        m=-1\n",
    "        for i ,num in enumerate(s):\n",
    "            if num not in h:\n",
    "                h[num]=i\n",
    "            else:\n",
    "                m=max(m,i-h[num]-1)\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        ans = -1\n",
    "        firstIndex = {}\n",
    "        for i, c in enumerate(s):\n",
    "            if c not in firstIndex:\n",
    "                firstIndex[c] = i\n",
    "            else:\n",
    "                ans = max(ans, i - firstIndex[c] - 1)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        maxs, n = -1, len(s)\n",
    "        for i in range(n - 1):\n",
    "            try:\n",
    "                end = s[i + 1:][::-1].index(s[i])\n",
    "                maxs = max(maxs, n - end - i - 2)\n",
    "            except ValueError:\n",
    "                continue\n",
    "        return maxs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        alp = [chr(i) for i in range(97,123)]\n",
    "        ans = []\n",
    "        if len(set(s)) == len(s):\n",
    "            return -1\n",
    "        for i in alp:\n",
    "            if i in s:\n",
    "                if s.rfind(i) == s.find(i):\n",
    "                    ans.append(0)\n",
    "                else:\n",
    "                    ans.append(s.rfind(i) - s.find(i) - 1)\n",
    "        return max(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        if len(s) <= 1 or len(set(s)) == len(s):\n",
    "            return -1\n",
    "        else:\n",
    "            sub_len_list = []\n",
    "            for idx, item in enumerate(s):\n",
    "                last_idx = s.rfind(item)\n",
    "                if last_idx > idx:\n",
    "                    sub_len_list.append(last_idx - idx - 1)\n",
    "                else:\n",
    "                    continue\n",
    "            return max(sub_len_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        ans = -1\n",
    "        firstIndex = {}\n",
    "        for i, c in enumerate(s):\n",
    "            if c not in firstIndex:\n",
    "                firstIndex[c] = i\n",
    "            else:\n",
    "                ans = max(ans, i - firstIndex[c] - 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        mp = defaultdict(list)\n",
    "        for i, c in enumerate(s):\n",
    "            mp[c].append(i)\n",
    "        return max(v[-1] - v[0] - 1 for v in mp.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        fre = [[] for _ in range(26)]\n",
    "        ans = 0\n",
    "        for i, ch in enumerate(s):\n",
    "            index = ord(ch) - ord('a')\n",
    "            if not fre[index]:\n",
    "                fre[index].append(i)\n",
    "            else:\n",
    "                ans = max(ans, i - fre[index][0])\n",
    "                fre[index].append(i)\n",
    "        return ans - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        max_result = 0\n",
    "        result_list = []\n",
    "        i = 0\n",
    "        n  = len(s) - 1\n",
    "        for i in range(n):\n",
    "            flag = False\n",
    "            for j in range(n, i, -1):\n",
    "                if s[i] == s[j]:\n",
    "                    flag = True\n",
    "                    if j - i - 1 not in result_list:\n",
    "                        result_list.append(j - i - 1)\n",
    "                    break\n",
    "                continue\n",
    "                \n",
    "            if not flag:\n",
    "                if -1 not in result_list:\n",
    "                    result_list.append(-1)\n",
    "                    \n",
    "        if len(result_list) == 1 and -1 in result_list:\n",
    "            return -1\n",
    "        else:\n",
    "            for result in result_list:\n",
    "                max_result = max(max_result, result)\n",
    "\n",
    "        return max_result\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "\"mgntdygtxrvxjnwksqhxuxtrv\"\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        dic = {}\n",
    "        res = -1\n",
    "        for index,item in enumerate(s):\n",
    "            if item not in dic:\n",
    "                dic[item] = index\n",
    "            else:\n",
    "                res = max(res,index-dic[item]-1)\n",
    "        return res\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        max_result = 0\n",
    "        result_list = []\n",
    "        i = 0\n",
    "        n  = len(s) - 1\n",
    "        while i < n:\n",
    "            flag = False\n",
    "            for j in range(n, i, -1):\n",
    "                if s[i] == s[j]:\n",
    "                    flag = True\n",
    "                    if j - i - 1 not in result_list:\n",
    "                        result_list.append(j - i - 1)\n",
    "                    break\n",
    "                continue\n",
    "                \n",
    "            if not flag:\n",
    "                if -1 not in result_list:\n",
    "                    result_list.append(-1)\n",
    "            i += 1\n",
    "        if len(result_list) == 1 and -1 in result_list:\n",
    "            return -1\n",
    "        else:\n",
    "            for result in result_list:\n",
    "                max_result = max(max_result, result)\n",
    "\n",
    "        return max_result\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        maxlen = -1\n",
    "        n = len(s)\n",
    "        for i in range(n):\n",
    "            for j in range(n-1, i, -1):\n",
    "                if s[i] == s[j]:\n",
    "                    maxlen = max(maxlen, j-i-1)\n",
    "        return maxlen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        if len(s)<=1:\n",
    "            return 0\n",
    "        ans=-1\n",
    "        size=len(s)\n",
    "        for i in range(len(s)):\n",
    "            if s[i] in s[i+1::]:\n",
    "                ans=max(ans,size-s[::-1].index(s[i])-i-2)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        if len(set(s))==len(s):return -1\n",
    "        print(set(s))\n",
    "        for ch in list(set(s)):\n",
    "            if s.count(ch)>1:\n",
    "                ans = max(ans,len(s)-1-s[::-1].index(ch)-s.index(ch)-1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        mp = defaultdict(list)\n",
    "        for i, e in enumerate(s):\n",
    "            mp[e].append(i)\n",
    "        return max(v[-1] - v[0] - 1 for v in mp.values() )\n",
    "             "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "\n",
    "        ans=-1\n",
    "        for left in range(len(s)):\n",
    "            for right in range(len(s)-1,left,-1):\n",
    "                if s[left]==s[right]:\n",
    "                    ans=max(ans,right-left-1)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        res=[]\n",
    "        for i in range(len(s)):\n",
    "            for j in range(i,len(s)):\n",
    "                count = 0\n",
    "                if s[i]==s[j]:\n",
    "                    count=j-i-1\n",
    "                    res.append(count)\n",
    "        return max(res)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:        \n",
    "        mlen=0\n",
    "        ad={}\n",
    "        for i in s:\n",
    "            ad[i]=ad.get(i,0)+1\n",
    "        j=[x for x in ad if ad[x]>1]\n",
    "        if not j:        \n",
    "            return -1\n",
    "\n",
    "        for i in s:\n",
    "            mlen=max(mlen,len(s)-s[::-1].index(i)-s.index(i)-2)        \n",
    "        return mlen\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        n = -1  # 用于保存子字符串的数量，初始值为-1是不存在子字符串的情况\n",
    "        for i in set(list(s)):  # 遍历字符串，set()可以去掉重复的字符\n",
    "            if s.count(i) > 1:  # 当出现次数大于2是再进行\n",
    "                n = max(n, s.rfind(i)-s.find(i)-1)  # s.rfind(i)查找最右侧的索引，s.find(i)查找最左侧的索引，再减1，得到子字符串的数量\n",
    "        return n\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        ans = -1\n",
    "        dic = {}\n",
    "        for i, c in enumerate(s):\n",
    "            if c not in dic:\n",
    "                dic[c] = i\n",
    "            else:\n",
    "                ans = max(ans, i - dic[c] - 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        return max(s.rfind(c) - s.find(c) - 1 for c in set(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        idxs = {}\n",
    "        ans = -1\n",
    "        for i, c in enumerate(s):\n",
    "            idxs[c] = idxs[c] if c in idxs else i\n",
    "            ans = max(ans, i - idxs[c] - 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        ans = -1 \n",
    "        first_index = {}\n",
    "        for i , c in enumerate(s) :\n",
    "            if c not in first_index :\n",
    "                first_index[c] = i \n",
    "            else :\n",
    "                ans = max(ans , i - first_index[c] - 1)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        if len(s)<2:\n",
    "            return 0\n",
    "        ans=-1\n",
    "        size=len(s)\n",
    "        for i in range(len(s)):\n",
    "            if s[i] in s[i+1::]:\n",
    "                ans=max(ans,size-s[::-1].index(s[i])-i-2)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        m=-1\n",
    "        for i in range(len(s)):\n",
    "            a=s[i]\n",
    "            for j in range(len(s)-1,i,-1):\n",
    "                b=s[j]\n",
    "                if a==b:\n",
    "                    l=j-i-1\n",
    "                    if l>m:\n",
    "                        m=l\n",
    "                    break\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        res = -1\n",
    "        for i in range(n-1):\n",
    "            for j in range(i+1, n):\n",
    "                if s[i] == s[j]:\n",
    "                    res = max(res, j - i - 1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        alp = [chr(i) for i in range(97,123)]\n",
    "        ans = []\n",
    "        if len(set(s)) == len(s):\n",
    "            return -1\n",
    "        for i in alp:\n",
    "            if i in s:\n",
    "                if s.rfind(i) == s.find(i):\n",
    "                    ans.append(0)\n",
    "                else:\n",
    "                    ans.append(s.rfind(i) - s.find(i) - 1)\n",
    "        return max(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        n=len(s)\n",
    "        res=-1\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                if s[i]==s[j]:\n",
    "                    res=max(res,j-i-1)\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        mp = defaultdict(list)\n",
    "        for i, c in enumerate(s):\n",
    "            mp[c].append(i)\n",
    "        return max(v[-1] - v[0] - 1 for v in mp.values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        max1 = -1\n",
    "        for i in range(len(s)):\n",
    "            for j in range(i+1,len(s)):\n",
    "                if s[i]==s[j]:\n",
    "                    if j-i-1>max1:\n",
    "                        max1 = j-1-i\n",
    "                    \n",
    "        return max1\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        save = set(s)\n",
    "        ans = -1\n",
    "        for i in save:\n",
    "            ans = max(ans,s.rfind(i)-s.find(i)-1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        hash={}\n",
    "\n",
    "        curv=0\n",
    "        maxv=-1             #这里必须是-1\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in hash:\n",
    "                hash[s[i]]=i            #该字符的最左边的位置\n",
    "            else:\n",
    "                curv=i-hash[s[i]]-1     #你找到了第二个就和第一个比，你找到第三个也是找第一个比，第四个也是\n",
    "                maxv=max(maxv,curv)\n",
    "        return maxv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        alp = [chr(i) for i in range(97,123)]\n",
    "        ans = []\n",
    "        if len(set(s)) == len(s):\n",
    "            return -1\n",
    "        for i in alp:\n",
    "            if i in s:\n",
    "                if s.rfind(i) == s.find(i):\n",
    "                    ans.append(0)\n",
    "                else:\n",
    "                    ans.append(s.rfind(i) - s.find(i) - 1)\n",
    "        return max(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        ans = -1\n",
    "        firstIndex = {}\n",
    "        for i, c in enumerate(s):\n",
    "            if c not in firstIndex:\n",
    "                firstIndex[c] = i\n",
    "            else:\n",
    "                ans = max(ans, i - firstIndex[c] - 1)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:        \n",
    "        mlen=0\n",
    "        ad={}\n",
    "        for i in s:\n",
    "            ad[i]=ad.get(i,0)+1\n",
    "        j=[x for x in ad.values()]\n",
    "        j.sort()\n",
    "        if j[-1]<2:        \n",
    "            return -1\n",
    "\n",
    "        for i in s:\n",
    "            mlen=max(mlen,len(s)-s[::-1].index(i)-s.index(i)-2)        \n",
    "        return mlen\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        alp = [chr(i) for i in range(97,123)]\n",
    "        ans = []\n",
    "        if len(set(s)) == len(s):\n",
    "            return -1\n",
    "        for i in alp:\n",
    "            if i in s:\n",
    "                if s.rfind(i) == s.find(i):\n",
    "                    ans.append(0)\n",
    "                else:\n",
    "                    ans.append(s.rfind(i) - s.find(i) - 1)\n",
    "        return max(ans,default=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        dic = {}\n",
    "        res = -1\n",
    "        for index,item in enumerate(s):\n",
    "            if item not in dic:\n",
    "                dic[item] = index\n",
    "            else:\n",
    "                res = max(res,index-dic[item]-1)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        length = len(s)\n",
    "        result = [-1] * length\n",
    "        for i in range(length):\n",
    "            for j in range(i + 1, length):\n",
    "                if s[j] == s[i]:\n",
    "                    result[i] = j - i - 1\n",
    "        return max(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        ans=-1\n",
    "        d={}\n",
    "        n=len(s)\n",
    "        for i in range(n):\n",
    "            if s[i] not in d:\n",
    "                d[s[i]]=i\n",
    "\n",
    "        for j in range(n-1,-1,-1):\n",
    "            if s[j] in d:\n",
    "                ans=max(ans,j-d[s[j]]-1)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "\n",
    "        ans=-1\n",
    "        for left in range(len(s)):\n",
    "            for right in range(len(s)-1,left,-1):\n",
    "                if s[left]==s[right]:\n",
    "                    ans=max(ans,right-left-1)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        ans = -1\n",
    "        firstIndex = {}\n",
    "        for i, c in enumerate(s):\n",
    "            if c not in firstIndex:\n",
    "                firstIndex[c] = i\n",
    "            else:\n",
    "                ans = max(ans, i - firstIndex[c] - 1)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        checklist = {}\n",
    "        result = -1\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in checklist.keys():\n",
    "                checklist[s[i]] = i\n",
    "            else:\n",
    "                result = max(i - checklist[s[i]] - 1, result)\n",
    "        \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        #暴力法\n",
    "        # n = len(s)\n",
    "        # maxlen = -1\n",
    "        # for i in range(n):\n",
    "        #     for j in range(n - 1, i, -1):\n",
    "        #         if s[i] == s[j]:\n",
    "        #             maxlen = max(maxlen, j - i - 1)\n",
    "        # return maxlen\n",
    "        lens = []\n",
    "        chmap = {}\n",
    "        for idx, ch in enumerate(s):\n",
    "            if ch not in chmap:\n",
    "                chmap[ch] = idx\n",
    "            else:\n",
    "                tmplen = idx - chmap[ch] - 1\n",
    "                lens.append(tmplen)\n",
    "        if len(lens) != 0:\n",
    "            return max(lens)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        if len(s) == 1:\n",
    "            return -1\n",
    "        res = -1\n",
    "        for i in range(len(s)):\n",
    "            j = len(s)-1\n",
    "            while j > i and j-i-1 > res:\n",
    "                if s[j] == s[i]:\n",
    "                    res = j-i-1\n",
    "                j -= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        m = dict()\n",
    "        ans = -1\n",
    "        for i in range(len(s)):\n",
    "            min_pos, max_pos = m.get(s[i], [-1, -1])\n",
    "            if min_pos == -1:\n",
    "                m[s[i]] = [i, -1]\n",
    "            else:\n",
    "                m[s[i]][1] = i\n",
    "            if m[s[i]][0] !=-1 and m[s[i]][1] != -1:\n",
    "                ans = max(ans, m[s[i]][1] - m[s[i]][0] - 1)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "\n",
    "        ans=-1\n",
    "        for left in range(len(s)):\n",
    "            if len(s)-left<ans:\n",
    "                break\n",
    "            for right in range(len(s)-1,left,-1):\n",
    "                if right-left-1<ans:\n",
    "                    break\n",
    "                if s[left]==s[right]:\n",
    "                    ans=right-left-1\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n",
    "        if len(s) <= 1 or len(set(s)) == len(s):\n",
    "            return -1\n",
    "        else:\n",
    "            ans = 0\n",
    "            for idx, item in enumerate(s):\n",
    "                last_idx = s.rfind(item)\n",
    "                if last_idx > idx and last_idx - idx - 1 > ans:\n",
    "                    ans = last_idx - idx - 1\n",
    "                else:\n",
    "                    continue\n",
    "            return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
